<template>
  <div class="dingdan-div">
    <el-card>
      <el-table border style="width: 100%; margin-top:20px" :data="orderList">
        <el-table-column label="创建日期" prop="createTime" width="100px"></el-table-column>
        <el-table-column label="订单编号" prop="id" width="100px"></el-table-column>
        <el-table-column label="商品名称" prop="productName" width="100px"></el-table-column>
        <el-table-column label="购买用户" prop="username" width="100px"></el-table-column>
        <el-table-column label="收货地址" prop="acceptAddress" width="500px"></el-table-column>
        <el-table-column label="是否已付款" prop="isPay">
          <template slot-scope="scope">
            <p v-if="scope.row.isPay == 0">未付款</p>
            <p v-else>已付款</p>
          </template>
        </el-table-column>
        <el-table-column label="是否已发货" prop="isSend">
          <template slot-scope="scope">
            <p v-if="scope.row.isPay == 0">未付款</p>
            <p v-else-if="scope.row.isSend == 1">已发货</p>
            <el-button v-else  type="primary" icon="el-icon-edit" @click="handleSend(scope.$index,scope.row)" ></el-button>
          </template>
        </el-table-column>
        <el-table-column label="是否已收货" prop="isAccept">
          <template slot-scope="scope">
            <p v-if="scope.row.isSend == 0">未发货</p>
            <p v-else-if="scope.row.isAccept == 0">未收货</p>
            <p v-else>已收货</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" @click="handleDelet(scope.$index,scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :current-page="currentPage"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLogin: false,
      currentPage: 1,
      orderList: []
    };
  },
  created() {},
  mounted() {
    this.currentPage = 1;
    this.getOrderList(this.currentPage);
  },
  methods: {
    //换页
    handleCurrentChange(val) {
      // console.log(val);
      this.currentPage = val;
      this.getOrderList(val);
    },
    //   获得所有订单
    async getOrderList(val) {
      const { data: res } = await this.$http.get("/admin/getOrderList/" + val);

      if (res.code == 200) {
        this.orderList = res.data.dingdanList;
      } else {
        this.$message({
          type: "info",
          message: "获取列表失败"
        });
      }
    },

    // 点击删除按钮
    handleDelet(index, row) {
      this.deletOrder(index, row);
    },

    // 删除订单
    async deletOrder(index, row) {
      // console.log(row)
      // console.log(index)
      this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.post(
            "/admin/deleteOrder",
            row
          );
          if (res.code == 200) {

            this.orderList.splice(index, 1);
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 发货
    handleSend(index, row){
      this.sendOrder(index,row);
    },
    sendOrder(index, row){
      this.$confirm("确定前请保证已发货", "提示", {
        confirmButtonText: "确认已发货",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          
          this.orderList[index].isSend = row.isSend = 1;
          const { data: res } = await this.$http.post(
            "/admin/sendProduct",
            row
          );
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "发货成功!"
            });
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消"
          });
        });
    }
  }
};
</script>

<style lang="less" scoped>
</style>
